home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 029 (1987-08-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 029 (1987-08-15)(Ossowski, Stefan)(DE)(PD).adf / FractalGenerator / Sc.d < prev    next >
Text File  |  1978-08-10  |  5KB  |  198 lines

  1. #include:intuition/miscellaneous.g
  2. #include:intuition/screen.g
  3. #include:intuition/window.g
  4. #include:intuition/intuitext.g
  5. #include:intuition/requester.g
  6. #include:graphics/gfx.g
  7. #include:graphics/view.g
  8. #include:graphics/rastport.g
  9. #include:libraries/dos.g
  10.  
  11. /*
  12.  * sc.d - fractalish terrain generator.
  13.  *
  14.  *    Date:      March 5, 1987 (original version sometime in 1985)
  15.  *    Author:      Chris Gray
  16.  *    Language: Draco
  17.  *    System:      Amiga
  18.  */
  19.  
  20. /*
  21.  * The nature of the terrain can be changed by playing with the numbers
  22.  * in 'Range'. If you change SIZE, you must also change the number of
  23.  * values given for 'Range'. The created terrain with SIZE = 8 is 256 pixels
  24.  * by 256 pixels, which doesn't fit on a non-interlaced screen, so only the
  25.  * top 200 pixels are displayed. The terrain is a torus, i.e. wraps around
  26.  * both vertically and horizontally.
  27.  */
  28.  
  29. /*
  30.  * Feel free to use this algorithm in any games you write, so long as you
  31.  * give me credit for it. (I THINK I invented it, since I've never heard of
  32.  * anything similar, and other programs I've seen use much slower methods.)
  33.  */
  34.  
  35. uint
  36.     SIZE = 8,
  37.     COUNT = 1 << SIZE,
  38.  
  39.     SCREEN_WIDTH = 320,
  40.     SCREEN_HEIGHT = 200,
  41.     SCREEN_DEPTH = 5,
  42.     COLOURS = 1 << SCREEN_DEPTH,
  43.     WINDOW_WIDTH = if COUNT < SCREEN_WIDTH then COUNT else SCREEN_WIDTH fi,
  44.     WINDOW_HEIGHT = if COUNT < SCREEN_HEIGHT then COUNT else SCREEN_HEIGHT fi;
  45.  
  46. [SIZE] uint Range = (32, 32, 32, 22, 14, 8, 4, 2);
  47.  
  48. [COLOURS] uint ColourMap = (
  49.     0x00f, 0x050, 0x070, 0x0a0, 0x0c0, 0x0e0, 0x4f4, 0x8f8,
  50.     0xdf0, 0xff0, 0xfd0, 0xfb0, 0xf90, 0xf70, 0xe50, 0xe33,
  51.     0xe11, 0xf01, 0xf03, 0xf05, 0xf07, 0xf09, 0xf0b, 0xf0d,
  52.     0xf1f, 0xf3f, 0xf5f, 0xf7f, 0xf9f, 0xfbf, 0xfdf, 0xfff
  53. );
  54.  
  55. *Screen_t Screen;
  56. *Window_t Window;
  57.  
  58. uint Seed;
  59.  
  60. [COUNT, COUNT] int Cell;
  61.  
  62. /*
  63.  * random - return a random number 0 - passed range.
  64.  */
  65.  
  66. proc random(uint rang)uint:
  67.  
  68.     if rang = 0 then
  69.     0
  70.     else
  71.     Seed := Seed * 17137 + 4287;
  72.     Seed := (Seed >> 8) >< (Seed << 8);
  73.     Seed % rang
  74.     fi
  75. corp;
  76.  
  77. /*
  78.  * set - set a given spot in Cell.
  79.  */
  80.  
  81. proc set(uint l, c, size; int height)void:
  82.     uint rang;
  83.  
  84.     rang := Range[size];
  85.     height := height + random(rang) - (rang + 1) / 2;
  86.     Cell[l, c] := height;
  87. corp;
  88.  
  89. /*
  90.  * grow - grow the basic scenery heights.
  91.  */
  92.  
  93. proc grow()void:
  94.     uint l, c, i, step, nextStep, l1, l2, c1, c2;
  95.  
  96.     Cell[0, 0] := 0;
  97.     step := COUNT;
  98.     for i from 0 upto SIZE - 1 do
  99.     nextStep := step / 2;
  100.     for l from 0 by step upto COUNT - 1 do
  101.         l1 := l + nextStep;
  102.         l2 := l + step;
  103.         if l2 = COUNT then
  104.         l2 := 0;
  105.         fi;
  106.         for c from 0 by step upto COUNT - 1 do
  107.         c1 := c + nextStep;
  108.         c2 := c + step;
  109.         if c2 = COUNT then
  110.             c2 := 0;
  111.         fi;
  112.         set(l, c1, i, (Cell[l, c] + Cell[l, c2] + 1) / 2);
  113.         set(l1, c, i, (Cell[l, c] + Cell[l2, c] + 1) / 2);
  114.         set(l1, c1, i, (Cell[l, c] + Cell[l, c2] +
  115.                 Cell[l2, c] + Cell[l2, c2] + 2) / 4);
  116.         od;
  117.     od;
  118.     step := nextStep;
  119.     od;
  120. corp;
  121.  
  122. /*
  123.  * display - display the resulting scenery.
  124.  */
  125.  
  126. proc display()void:
  127.     uint l, c;
  128.     int height;
  129.  
  130.     for l from 0 upto WINDOW_HEIGHT - 1 do
  131.     for c from 0 upto WINDOW_WIDTH - 1 do
  132.         height := Cell[l, c];
  133.         SetAPen(Window*.w_RPort,
  134.             if height < 0 then
  135.             0
  136.             elif height >= COLOURS then
  137.             COLOURS - 1
  138.             else
  139.             height
  140.             fi);
  141.         pretend(WritePixel(Window*.w_RPort, c, l), void);
  142.     od;
  143.     od;
  144. corp;
  145.  
  146. /*
  147.  * main program.
  148.  */
  149.  
  150. proc main()void:
  151.     NewWindow_t newWindow;
  152.     DateStamp_t ds;
  153.     IntuiText_t bodyText, positiveText, negativeText;
  154.  
  155.     bodyText := IntuiText_t(COLOURS - 1, 0, 0, 51, 5, nil, nil, nil);
  156.     bodyText.it_IText := "Done";
  157.     positiveText := IntuiText_t(COLOURS - 1, 0, 0, 7, 3, nil, nil, nil);
  158.     positiveText.it_IText := "Next";
  159.     negativeText := IntuiText_t(COLOURS - 1, 0, 0, 7, 3, nil, nil, nil);
  160.     negativeText.it_IText := "Quit";
  161.     if OpenIntuitionLibrary(0) ~= nil then
  162.     if OpenGraphicsLibrary(0) ~= nil then
  163.         if OpenDosLibrary(0) ~= nil then
  164.         Screen := OpenScreen(&NewScreen_t(
  165.             0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH, 0, 1,
  166.             0x0, CUSTOMSCREEN, nil, nil, nil, nil));
  167.         if Screen ~= nil then
  168.             LoadRGB4(&Screen*.sc_ViewPort, &ColourMap[0], COLOURS);
  169.             newWindow := NewWindow_t(
  170.             0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
  171.             FREEPEN, FREEPEN,
  172.             0x0, BORDERLESS | ACTIVATE | NOCAREREFRESH,
  173.             nil, nil, nil, nil, nil, 0, 0, 0, 0,
  174.             CUSTOMSCREEN);
  175.             newWindow.nw_Screen := Screen;
  176.             Window := OpenWindow(&newWindow);
  177.             if Window ~= nil then
  178.             DateStamp(&ds);
  179.             Seed := (ds.ds_Minute >< ds.ds_Tick) | 1;
  180.             while
  181.                 grow();
  182.                 display();
  183.                 AutoRequest(Window, &bodyText, &positiveText,
  184.                     &negativeText, 0x0, 0x0, 150, 50)
  185.             do
  186.             od;
  187.             CloseWindow(Window);
  188.             fi;
  189.             CloseScreen(Screen);
  190.         fi;
  191.         CloseDosLibrary();
  192.         fi;
  193.         CloseGraphicsLibrary();
  194.     fi;
  195.     CloseIntuitionLibrary();
  196.     fi;
  197. corp;
  198.